package es.upm.emse.softdesign.controltower.model.aircraft.imp;
import java.util.List;

import org.apache.log4j.Logger;

import es.upm.emse.softdesign.controltower.model.aircraft.Aircraft;
import es.upm.emse.softdesign.controltower.model.airport.LandingField;
import es.upm.emse.softdesign.controltower.model.exception.AircraftOperationException;
import es.upm.emse.softdesign.controltower.model.library.PlaneType;
import es.upm.emse.softdesign.controltower.model.queue.AirplaneQueue;

/**
 * Represent a Plane
 * @author hades
 * @version 1.0
 * @created 20-mar-2013 0:49:56
 */
public class Plane extends Aircraft {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static Logger logger = Logger.getLogger(Plane.class);
	
	private String model;
	private PlaneType subType;
	/**
	 * Constructor
	 * @param id {@link String}
	 * @param fuelRemaning int 
	 * @param subtype {@link PlaneType}
	 */
	public Plane(String id, int fuelRemaning, PlaneType subtype){
		super(id,fuelRemaning);
		this.subType = subtype;
	}

	public String getModel() {
		return model;
	}

	public void setModel(String model) {
		this.model = model;
	}

	
	@Override
	/*
	 * (non-Javadoc)
	 * @see es.upm.emse.softdesign.controltower.model.aircraft.Aircraft#getSubtype()
	 */
	public Enum<? extends PlaneType>  getSubtype() {
		
		return this.subType;
	}
	
	/*
	 * (non-Javadoc)
	 * @see es.upm.emse.softdesign.controltower.model.aircraft.Aircraft#land(java.util.List, es.upm.emse.softdesign.controltower.model.queue.AirplaneQueue)
	 */
	 public void land(List<LandingField> landingFields,
				AirplaneQueue landingQueue) throws AircraftOperationException{
			
			// Serch for the first landingField available
			boolean allOccupy = true;

			try {
				for (LandingField field : landingFields) {
					// verify if exist one landing field available
					try {
						field.setAircraftOnField(this);
						logger.info("The aircraft :" + this.getId()
								+ " has landed in " + field.getNumber() + "at: "
								+ field.getTimeOfLastLanding());
						allOccupy = false;
						break;
					} catch (AircraftOperationException e) {
						logger.warn(e.getMessage());
					}
				}
				// id all the fields are occupy i put again the plane into the queue
				if (allOccupy) {
					logger.info("The aircraft :" + this.getId()
							+ "is back to the landing queue");
					landingQueue.putPlane(this);
					
				}
			} catch (Exception e) {
				logger.error(
						AircraftOperationException.LANDING_ERROR + this.getId(),
						e);
				throw new AircraftOperationException(AircraftOperationException.LANDING_ERROR
						+ this.getId(), e);
			}
		}

	@Override
	/*
	 * (non-Javadoc)
	 * @see es.upm.emse.softdesign.controltower.model.aircraft.Aircraft#prerareTakeOff(java.util.List, es.upm.emse.softdesign.controltower.model.queue.AirplaneQueue)
	 */
	public void prerareTakeOff(List<LandingField> landingFields,
			AirplaneQueue parkingQueue) throws AircraftOperationException {
		// TODO put the logic here
		
	}

	@Override
	/*
	 * (non-Javadoc)
	 * @see es.upm.emse.softdesign.controltower.model.aircraft.Aircraft#park(es.upm.emse.softdesign.controltower.model.aircraft.Aircraft, es.upm.emse.softdesign.controltower.model.queue.AirplaneQueue)
	 */
	public void park(Aircraft otherAircraft, AirplaneQueue parkinQueue)
			throws AircraftOperationException {
		// TODO put the logic here
		
	}

	@Override
	/*
	 * (non-Javadoc)
	 * @see es.upm.emse.softdesign.controltower.model.aircraft.Aircraft#takeOff(es.upm.emse.softdesign.controltower.model.aircraft.Aircraft, es.upm.emse.softdesign.controltower.model.airport.LandingField)
	 */
	public void takeOff(Aircraft otherAircraft, LandingField landingField)
			throws AircraftOperationException {
		// TODO put the logic here
		
	}

	@Override
	/*
	 * (non-Javadoc)
	 * @see es.upm.emse.softdesign.controltower.model.aircraft.Aircraft#forcePark(es.upm.emse.softdesign.controltower.model.queue.AirplaneQueue, es.upm.emse.softdesign.controltower.model.airport.LandingField)
	 */
	public void forcePark(AirplaneQueue parkinQueue, LandingField field)
			throws AircraftOperationException {
		// TODO put the logic here
		
	}

	@Override
	/*
	 * (non-Javadoc)
	 * @see es.upm.emse.softdesign.controltower.model.aircraft.Aircraft#forceTakeOff(es.upm.emse.softdesign.controltower.model.airport.LandingField)
	 */
	public void forceTakeOff(LandingField landingField)
			throws AircraftOperationException {
		// TODO put the logic here
		
	}


}